package cn.stat.controller;

import cn.stat.domain.BaseEntity;
import cn.stat.service.StatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mxb
 * @version 1.0
 * @date 2019/11/5 16:13
 * 统计管理菜单 路由
 */
@Controller
@RequestMapping("/stat")
public class StatController {

    @Autowired
    private StatService statService;

    //跳转客户地区统计
    @RequestMapping("toAreaStat")
    public String toCustomerAreaStat() {
        return "stat/customerAreaStat";
    }

    /**
     * 加载客户地区数据
     * @return
     */
    @RequestMapping("/loadCustomerAreaStatJson")
    @ResponseBody
    public List<BaseEntity> loadCustomerAreaStatJson() {
        return this.statService.loadCustomerAreaStatList();
    }

    //跳转客户地区统计
    @RequestMapping("toOpernameYearGradeStat")
    public String toOpernameYearGradeStat() {
        return "stat/opernameYearGradeStat";
    }

    //查询业务员年度业绩
    @RequestMapping("loadOpernameYearGradeStat")
    @ResponseBody
    public Map<String, Object> loadOpernameYearGradeStat(String year) {
        List<BaseEntity> entities = this.statService.loadOpernameYearGradeStatList(year);

        Map<String, Object> map = new HashMap<String, Object>();
        List<String> names = new ArrayList<String>();
        List<Double> values = new ArrayList<Double>();
        for (BaseEntity baseEntity : entities) {
            names.add(baseEntity.getName());
            values.add(baseEntity.getValue());
        }
        map.put("name", names);
        map.put("value", values);
        return map;
    }

    //跳转
    //跳转客户地区统计
    @RequestMapping("toCompanyYearGradeStat")
    public String toCompanyYearGradeStat() {
        return "stat/companyYearGradeStat";
    }

    //加载业务员年度统计数据
    @RequestMapping("loadCompanyYearGradeStat")
    @ResponseBody
    public List<Double> loadCompanyYearGradeStat(String year) {
        List<Double> doubles = this.statService.queryCompanyYearGradeStat(year);

        // 循环
        for (int i = 0; i < doubles.size(); i++) {
            if (doubles.get(i) == null) {
                doubles.set(i, 0.0);
            }
        }
        return doubles;
    }

}
